define([ "jquery" ],
    function(jq) {
      jq.r= (function(jq) {
        /*
         * 插件注册工具
         */
        var DEFAULT_METHODS = {
          init : function() {
            console.log("init")
          },
          getBiz : function() {
            return this;
          },
          getSettings : function() {
            return this.settings;
          },
          on : function() {
            return jq(this).on.apply($this, arguments);
          },
          bind : function() {
            return jq(this).bind.apply($this, arguments);
          },
          trigger : function() {
            return jq(this).trigger.apply($this, arguments);
          },
          delegate : function() {
            return jq(this).delegate.apply($this, arguments);
          }
        };

        var func_interface = function(clz) {
          var _clz = clz;
          // 生成每个插件的UUID
          (!_clz.UUID) && (_clz.UUID = uuid());
          _clz.prototype.UUID=_clz.UUID;
          // 添加默认函数.
          var tmp = {};
          jq.extend(tmp, DEFAULT_METHODS);
          jq.extend(tmp, _clz.prototype);
          jq.extend(_clz.prototype, DEFAULT_METHODS);

          var execfunc = function(funcName) {
            var args = (funcName ? Array.prototype.slice.call(arguments, 1)
                : []);
            var res = [];
            this.each(function(i) {

              var obj = null;
              var f_type = (!funcName) || typeof funcName == "string";

              if (f_type) {
                obj = $.register("find", this, _clz);
              } else {
                obj = $.register("find", this, _clz, funcName);
                var __this=this;;
                $(this).on("_distory",function(ev,data){
                  //console.log("UUID:"+_clz.UUID)
                  //console.log("data UUID:"+data.UUID)
                  if(data.UUID!=_clz.UUID){
                    $.removeData(obj,_clz.UUID);
                    $.removeData(__this,_clz.UUID);
                  }
                });
                return this;
              }
              
              

              var tmp = f_type && funcName && obj[funcName] && obj
                  && obj[funcName].apply(obj, args.slice());
              if (tmp) {
                res[i] = tmp;
              }
            });
            var result = (res.length == 1 ? res[0] : (res.length > 1 ? res
                : this));
            return result;
          };

          jq.extend(execfunc, {
            _over_write : function(funcName, superFunc, func) {
              // 重写插件方法
              if (!superFunc) {
                superFunc = "_" + funcName + "$" + sUuid();
              }
              _clz.prototype[funcName]
                  && (_clz.prototype[superFunc] = _clz.prototype[funcName]);
              _clz.prototype[funcName] = func;
            },
            _get_class : function() {
              return _clz;
            }
          });
          return execfunc;
        };
        return func_interface;
      })(jq);
      
      
      window.regPlugin=function(clz){
        return jq.fn[clz.name.substr(0,1).toLowerCase()+clz.name.substr(1)]=jq.r(clz);
      }
      
      /**
       * 定义一个jQuery插件.
       * @param name 插件名称.
       * @param parents 插件类集成的父类.
       * <p>可以为数组</p>
       * @param constructor 插件类的构造函数.
       */
      window.definePlugin=function(name,parents,constructor){
        var clz = defineClass(parents,constructor,name);
        jq.fn[name.substr(0,1).toLowerCase()+name.substr(1)]=jq.r(clz);
        return clz;
      }
      
      jq.regPlugin=window.regPlugin;
      jq.definePlugin=window.definePlugin;
      
      return jq;
    });